require 'libluartk'
gx=require 'libgamex'
--tprint(gx)
tparser=require 'libgamex.tpl_parser'
B=require('wxExt.ui_builder')
dbg=require('wxExt.debug')

local function xmlPrint(t)
	print('<?xml version="1.0" encoding="UTF-8"?>')
	print('<object>')
	print(rtk.tableToXml(t))
	print('</object>')
end

function t1()
	A={
		int1={FOffset=0,type='int32'},
		int2={FOffset=4,type='int32'},
		int3={FOffset=8,type='int32'},
		int4={FOffset=12,type='int32'},
	}
	buffer='\99\0\0\0\33\0\0\0\3\0\0\0\4\0\0\0'
	obj=gx.CAccessor.create(A)
	rtk.tprint('metatable',getmetatable(obj))
	print(obj)
	obj:bind(buffer)
	print('bouned')
	print(obj.int1,obj.int2)
end

function t2()
	t3()
	t4()
	obj:bind(bufobj)
	print('exp=',obj.经验值,';')
	rtk.printf('attack=%d;',obj.四围.进攻)
	obj.四围.进攻=obj.四围.进攻+1
	rtk.printf('attack=%d;',obj.四围.进攻)
	rtk.tprint(obj.四围:toTable())
end

function t3()
	odefs=tparser.load_winhex_template('heroes3/data/hero.tpl')
	rtk.tprint(odefs)
	obj=gx.CAccessor.create(odefs.HERO)
	obj:setEncoding('gb2312')
end

function t31()
	odefs=tparser.load_winhex_template('heroes3/data/hero.tpl')
	xmlPrint(odefs)
end

function t4()
	local fh=io.open('heroes3/test/hero.bin','rb')
	local buffer=fh:read('*a')
	fh:close()
	bufobj=gx.CBuffer.create()
	bufobj:load(buffer)
	print(#bufobj,bufobj)
end

function t6(addr)
	odefs=tparser.load_winhex_template('heroes3/hero.tpl')
	buf=gx.CBuffer.create()
	buf:load(gx:peek(addr,odefs.HERO.FSize))
	obj=gx.CAccessor.create(odefs.HERO,buf)
	if t5()==wx.wxID_OK then
		gx:poke(addr,buf:toString())
	end
end

function t7()
	str='abcde123'
	hs=gx.string_to_hex(str)
	ostr=gx.string_from_hex(hs)
	rtk.tprint{str,hs,ostr}
end

function t8()
	local fh=io.open('heroes3/test/hero.bin','rb')
	local buffer=fh:read('*a')
	fh:close()
	require 'heroes3.heroes3'
	show_hero(buffer)
end
